package com.tzg.iot.http.service;

import com.alibaba.fastjson.JSONObject;
import com.tzg.iot.http.info.HostInfo;
import com.tzg.iot.http.util.HttpClientUtil;
import com.tzg.iot.http.util.TokenUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * 上传属性/遥测数据
 * @author tzg157
 */
public class SendDataService {

    /**
     * 上传属性数据
     */
    public void sendAttributeData(String credentials,String jsonData) throws IOException {
        String url = HostInfo.HTTP_PROTOCOL + HostInfo.HOST + ":" + HostInfo.PORT + HostInfo.TENANT_DEVICE_ATTRIBUTES;
        url = String.format(url,credentials);
        InputStream inputStream =
                HttpClientUtil.post(url,jsonData);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String str = reader.readLine();
        System.out.println(str);
//        Map<String,Object> data = JSONObject.parseObject(credentialStr,Map.class);
//        if(data != null){
//            return String.valueOf(data.get("credentialsId"));
//        }
//        return "";
    }

    /**
     * 上传遥测数据
     */
    public void sendTelemetryData(String credentials,String jsonData) throws IOException {
        String url = HostInfo.HTTP_PROTOCOL + HostInfo.HOST + ":" + HostInfo.PORT + HostInfo.TENANT_DEVICE_TELEMETRY;
        url = String.format(url,credentials);
        InputStream inputStream =
                HttpClientUtil.post(url,jsonData);
    }
}
